package com.example.demo;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.concurrent.Semaphore;

/**
 * @author ChenXiaoBin
 * on 2020-09-06
 */
public class SemaphoreLock {


    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(2);

        new Thread(() -> {
            try {
                semaphore.acquire();
                System.out.println("T1 Running......");
                Thread.sleep(200);
                System.out.println("T1.2 Running......");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                semaphore.release();
            }
        }).start();


        new Thread(() -> {
            try {
                semaphore.acquire();
                System.out.println("T2 Running......");
                Thread.sleep(200);
                System.out.println("T2.2 Running......");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                semaphore.release();
            }
        }).start();
    }
}
